package toy.keli.picmap.tools;

import android.view.View;
import android.widget.DatePicker;

import com.baidu.mapsdkexample.geometry.markercluster.MyItem;

import java.util.ArrayList;
import java.util.List;

import toy.keli.picmap.R;
import toy.keli.picmap.model.Model;
import toy.keli.picmap.model.pic;
import toy.keli.picmap.model.time;
import toy.keli.picmap.util.utilCallback;

public class Filter extends  Tools{

    public void hideMarkers(){
        Control.picShowEnable=false;
        act.addMarkers();
    }
    public void showMarkers(){
        Control.picShowEnable=true;
        act.addMarkers();
    }
    public List<MyItem> runByDate() {
        return runByDate(Control.date[0],Control.date[1]);
    }

    public List<MyItem> runByDate(long start) {
        return runByDate(start,new time().getTime());
    }

    public List<MyItem> runByDate(utilCallback.picFilter func) {
        act.mClusterManager.clearItems();
        List<MyItem> items=new ArrayList<MyItem>();
        for(MyItem item: act.mItems) {
            boolean removeFlag = false;
            pic p=(pic) Model.populateRecord(pic.class,item.getData());
            if(func.call(p)) {
                items.add(item);
                act.mClusterManager.addItem(item);
            }
        }
        act.refresh();
        return items;
    }

    public List<MyItem> runByDate(long start, long end) {
        if(start!=0 && end==0) end=start+86400000;
        //清除所有页面点元素
        act.mClusterManager.clearItems();
        List<MyItem> items=new ArrayList<MyItem>();
        for(MyItem item: act.mItems) {
            boolean removeFlag = false;
            pic p=(pic)Model.populateRecord(pic.class,item.getData());
            //日期过滤
            if(start != 0){
                removeFlag=!(p.intime>start && p.intime<end);
            }
            //关键词过滤
            if(!removeFlag && Control.key.length()>0){
                removeFlag=p.name.matches(Control.key);
            }
            if(removeFlag) {
                // sites.remove(item.id);
            }else {
                //添加点信息
                act.mClusterManager.addItem(item);
                items.add(item);
            }
        }
//        mItems.clear();
//        mItems.addAll(items);
        //addMarkers();
        act.refresh();
        act.mItemsFilter=items;
        return items;
    }


    public void selectDay(utilCallback.timeLimit func){
        DatePicker datePicker1 = (DatePicker) act.findViewById(R.id.date_pick1);
        datePicker1.setVisibility(View.VISIBLE);
        time t = new time();
        //datePicker1.updateDate(t.formatInt("yyyy"),t.getMonth(),t.getDay());
        //设置默认的时间 比如2055年 9月9日
        datePicker1.init(t.getYear()+1900, t.getMonth(), t.formatInt("dd"), new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                t.setYear(year-1900);
                t.setMonth(monthOfYear);
                t.setDate(dayOfMonth);
                long start=t.getTime("yyyy-MM-dd"),end=start+86400*1000;
                //runByDate(start,end);
                func.call(start,end);
                datePicker1.setVisibility(View.GONE);
            }
        });
    }
}
